package com.le.tester.imooc.masterJUC.cas;

/**
 * 描述：CAS,指令操作实现
 */
public class SimulatedCAS {

    private String string;
    private volatile int value;

    //    如果一个内存地址V，V和A进行比较，如果V==A,那么将V的值更新为B
    public synchronized int compareAndSwap(int expectedValue, int newValue) {
        int oldValue = value;
        if (oldValue == expectedValue) {
            value = newValue;
        }
        return oldValue;
    }

    public int getValue() {
        return value;
    }
}
